<?php

class Sys_SysHelpDataController extends Zend_Controller_Action
{

    private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();
		
		$this->common->checklogin(true, true);

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Base();
        /* Initialize action controller here */
    }

	public function getlistAction()
	{
		$start = $this->request->getParam("start");
		$limit = $this->request->getParam("limit");
		$_iAmount = $this->db->fetchOne("SELECT count(*) FROM syshelpdata WHERE status & 1 = 0");
		$result = $this->db->fetchAll("SELECT d.*, c.Subject as CatName FROM syshelpdata d INNER JOIN syshelpcat c ON d.CatID = c.CatID WHERE d.status & 1 = 0 limit $start, $limit");
		$sMsg = "";
		$len = count($result);
		for($i=0; $i < $len; $i++) {
			$comma = ",";
			if($i == ($len -1)) 
			{
				$comma = "";
			}
			$sMsg .= "{ArticleID:" . $result[$i]["ArticleID"] . ", CatName:'" . $result[$i]["CatName"] . "', CatID:" . $result[$i]["CatID"] . ",Subject: '" .$result[$i]["Subject"]. "', Keyword: '" . $result[$i]["Keyword"] . "', ViewCount: ".$result[$i]["ViewCount"].", ListIndex:" . $result[$i]["ListIndex"] . ", Revision:'" . $result[$i]["Revision"] . "', OptionSet: " . $result[$i]["OptionSet"] . ",UpdateOn: '" .$this->common->getLocaldate($result[$i]["UpdateOn"]). "',CreateOn:'" . $this->common->getLocaldate($result[$i]["CreateOn"]) . "', Status:" . $result[$i]["Status"] . " }" . $comma;
		}

		$acl = array("aRead" => 1, "aNew" => 1, "aEdit" => 1, "aLock" => 1, "aDel" => 1, "aList" => 1);
		$_sExtJSD = "Acl:[{aRead:" . $acl["aRead"] . ", aNew:" . $acl["aNew"] . ",aEdit:" . $acl["aEdit"] . ",aDel:" . $acl["aDel"] .
							  ",aList:" . $acl["aList"] . "}]";
		$this->common->PutData('{totalProperty:' . $_iAmount . ',records:[' . $sMsg . '],' . $_sExtJSD . '}');
		exit;
	}

	public function saveAction()
	{
		$ArticleID = $this->request->getParam("ArticleID");
			$CatID = $this->request->getParam("CatID");
			$Subject = $this->request->getParam("Subject");
			$Keyword = $this->request->getParam("Keyword");
			
			$Content = $this->request->getParam("Content");
			$ListIndex = $this->request->getParam("ListIndex");
			$ListIndex = $ListIndex == "" ? 0 : $ListIndex;
			$Revision = $this->request->getParam("Revision");
			$Revision = $Revision == "" ? 0 : $Revision;
			$Remark = $this->request->getParam("Remark");
			$userid = $this->common->getUserid();

		if($ArticleID > 0) {
			if($this->db->update("syshelpdata", array(
					"CatID"   => $CatID,
					"Subject" => $Subject,
					"Keyword"  => $Keyword,
					"Content"  => $Content,
					"ListIndex" => $ListIndex,
					"Revision"  => $Revision,
					"Remark"    => $Remark
				), $this->db->quoteInto('ArticleID = ?', $ArticleID)))
				{
					$this->common->PutData('{result:""}');
				}
				else
				{
					$this->common->PutData('{error:"保存失败!"}');
				}
		}
		else
		{
			if($this->db->insert("syshelpdata", array(
				"CatID"   => $CatID,
					"Subject" => $Subject,
					"Keyword"  => $Keyword,
					"Content"  => $Content,
				"ViewCount"  => 0,

					"ListIndex" => $ListIndex,
					"Revision"  => $Revision,
					"Remark"    => $Remark,
				"OptionSet"	=> 0,
						"UpdateOn" => $this->common->getGmtime(),
				"CreateOn"  => $this->common->getGmtime(),
				"Status"	=> 0
			)) >= 0) {
				
				$this->common->PutData('{result:""}');
			}
			else
			{
				$this->common->PutData('{error:"保存失败!"}');
			}
		}

		exit;
	}

	public function delAction()
	{
		$uids = $this->request->getParam("aid");
		
		if($this->db->update("syshelpdata", array(
					"status" => 1
				), $this->db->quoteInto('ArticleID in (?)', $uids))) 
		{
			
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

	public function lockAction()
	{
		$uids = $this->request->getParam("aid");
		
		$stmt = $this->db->query("UPDATE syshelpdata SET status = 2 - status & 2 WHERE status in (0,2) AND " . $this->db->quoteInto('ArticleID in (?)', $uids));
		if($stmt->rowCount())
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

}

